package com.xray.netdisk.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.xray.netdisk.pojo.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 使用DBUtils包实现的工具类
 */
public class JDBCUtils {

    //c3p0连接池
    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    //数据库操作类
    private static QueryRunner runner = new QueryRunner(dataSource);

    /**
     * 增删改
     * @param sql
     * @param args
     * @return
     */
    public static int update(String sql,Object... args){
        try {
            return runner.update(sql, args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 查询
     * @param clazz
     * @param sql
     * @param args
     * @return
     */
    public static <T> List<T> queryList(Class<T> clazz, String sql, Object... args){
        try {
            return runner.query(sql,new BeanListHandler<T>(clazz),args);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static <T> List<T> queryList2(Class<T> clazz, String sql, Object... args) throws Exception {
        List<T> list = new ArrayList<>();
        Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for(int i = 0;i < args.length;i++){
            preparedStatement.setObject(i + 1,args[i]);
        }
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            T obj = clazz.newInstance();
            Field[] fields = clazz.getDeclaredFields();
            for(Field field : fields){
                String name = field.getName();
                Object value = resultSet.getObject(name);
                field.setAccessible(true);
                field.set(obj,cast(field.getType(),value));
            }
            list.add(obj);
        }
        return list;
    }

    public static Object cast(Class type,Object value){
        Object val = null;
        if(type == Integer.class){
            val = Integer.valueOf(value.toString());
        }else if(type == Long.class){
            val = Long.valueOf(value.toString());
        }else if(type == Double.class){
            val = Double.valueOf(value.toString());
        }else if(type == Float.class){
            val = Float.valueOf(value.toString());
        }else if(type == Boolean.class){
            val = Boolean.valueOf(value.toString());
        }else if(type == Byte.class){
            val = Byte.valueOf(value.toString());
        }else if(type == Short.class){
            val = Short.valueOf(value.toString());
        }else if(type == Character.class){
            val = Character.valueOf(value.toString().charAt(0));
        }else{
            val = value;
        }
        return val;
    }

    public static void main(String[] args) throws Exception {
        List<User> users = queryList2(User.class, "select * from t_user");
        System.out.println(users);
    }
}
